package com.wave.lib.pull;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.wave.lib.SwipeListView;

/**
 *
 * PullToRefreshSwipeListView.java
 * @author Wave.Zhang 
 * @data 2015-5-12 t下午3:50:21
 * 
 */
public class PullToRefreshSwipeListView extends PullToRefreshBase<SwipeListView>{

	public PullToRefreshSwipeListView(Context context, AttributeSet attrs) {
		super(context, attrs);
	}

	@Override
	public Orientation getPullToRefreshScrollDirection() {
		return Orientation.VERTICAL;
	}

	@Override
	protected SwipeListView createRefreshableView(Context context,AttributeSet attrs) {
		return new SwipeListView(context,attrs);
	}

	@Override
	protected boolean isReadyForPullEnd() {
		boolean result = false;
	    int last = getRefreshableView().getChildCount() - 1;
	    View v = getRefreshableView().getChildAt(last);

	    int firstVisiblePosition = getRefreshableView().getFirstVisiblePosition();
	    int visibleItemCount = getRefreshableView().getChildCount();
	    int itemCount = getRefreshableView().getAdapter().getCount();
	    if (firstVisiblePosition + visibleItemCount >= itemCount) {
	      if (v != null) {
	        boolean isLastFullyVisible = v.getBottom() <= getRefreshableView().getHeight();
	        result = isLastFullyVisible;
	      }
	    }
	    return result;
	}

	@Override
	protected boolean isReadyForPullStart() {
		SwipeListView mSwipeMenuListView = getRefreshableView();
		Log.d("isReadyForPullStart", "isopen ====" + mSwipeMenuListView.isOpenMenu());
		if(mSwipeMenuListView != null && mSwipeMenuListView != null){
//			if(mSwipeMenuListView.getFirstVisiblePosition() <= 0 && !mSwipeMenuListView.isOpenMenu()){
//				return true;
//			}
            if(mSwipeMenuListView.getFirstVisiblePosition() <= 0 && !mSwipeMenuListView.isOpenMenu()&&mSwipeMenuListView.getChildCount() > 0  && mSwipeMenuListView.getChildAt(0).getTop() >= 0){
                return true;
            }
		}
		return false;
	}
}
